package org.thirdHero.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thirdHero.core.exception.TokenErrorException;
import org.thirdHero.core.util.ResponseUtil;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    private final Log logger = LogFactory.getLog(GlobalDefaultExceptionHandler.class);

	//声明要捕获的异常
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Object defaultExceptionHandler( Exception e) {
		e.printStackTrace();
		if(e instanceof TokenErrorException) {
			logger.error("======token不存在或过期===");
            TokenErrorException tokenErrorException = (TokenErrorException)e;
			return ResponseUtil.fail(tokenErrorException.getCode(),tokenErrorException.getMessage());
		}
		//未知错误
		return ResponseUtil.fail("系统异常");
	}
}